<html>
<body>
<p>Test IsProviderInstalled.<p>
<div id=result>
</div>
<script>
var passedAll = true;

function log(message) {
  document.getElementById("result").innerHTML += message + "<br>";
}

function logPassed(message) {
  log("PASS: " + message);
}

function logFailed(message) {
  passedAll = false;
  log("FAIL: " + message);
}

function verifyExceptionFor(testName, origin) {
  try {
    window.external.IsSearchProviderInstalled(origin);
    logFailed("No exception for a " + testName + " (" + origin + ").");
  } catch (e) {
    logPassed("Got an exception for a " + testName + " (" + origin + ").");
  }
}

function writeResult() {
  var result = "1";
  if (passedAll) {
    logPassed("Everything passed.");
    document.title = "OK";
  } else {
    logFailed("At least one test failed.");
    document.title = "FAIL"
  }
}

function main() {
  try {
    var differentProtocol =
        document.location.protocol == "http:" ? "https:" : "http:";
    var differentPort =
        (!document.location.port || document.location.port == "80") ? ":81" : ":80";

    var origin =
        document.location.protocol + "//" + document.location.host + "/";
    var originWithDifferentProtocol = differentProtocol + "//" +
        document.location.host + "/";
    var originWithDifferentPort = document.location.protocol + "//" +
        document.location.hostname + differentPort + "/";

    // Verify existence of the api.
    var foundApi = false;
    try {
      if (window.external.IsSearchProviderInstalled)
        foundApi = true;
    } catch (e) {}

    if (foundApi) {
      logPassed("IsSearchProvider api exists.");
    } else {
      logFailed("IsSearchProvider api doesn't exist.");
      writeResult();
      return;
    }

    // Verify the search provider state for the current page.
    var installed = window.external.IsSearchProviderInstalled(origin)
    var installedMessage = "Search provider ("+ origin +"): " + installed + ".";
    if (installed == document.location.hash.substring(1)) {
      logPassed(installedMessage);
    } else {
      logFailed(installedMessage +
          " The expected result is passed as the hash.");
    }

    // Verify that cases that should result in exceptions.
    verifyExceptionFor("different host", "http://example.org/");
    verifyExceptionFor("different protocol", originWithDifferentProtocol);
    verifyExceptionFor("different port", originWithDifferentPort);

    writeResult();
  } catch (e) {
    logFailed("An exception occurred. Name: " + e.name + " Message: " +
              e.message);
    writeResult();
  }
}
main();
</script>
</body>
</html>
